﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNet4.Utilities;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;

public partial class Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //string url = HttpContext.Current.Request.Url.Query;
        //url = url.Substring(1);
        //string[] parameters = url.Split('&');

        //string code = parameters[0].Split('=')[1];
        //string url2 = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx895ec6360c6d98cd&secret=2d7144c884fe68bae31af51c2e9c5f7c&code=" + code + "&grant_type=authorization_code";

        //string result = C5HttpHelperGetResult(null, null, url2);

        //Url2 model = null;
        //using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(result)))
        //{
        //    DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(typeof(Url2));
        //    model = (Url2)deseralizer.ReadObject(ms);// //反序列化ReadObject
        //}

        //string url3 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + model.access_token + "&openid=" + model.openid + "&lang=zh_CN";

        //result = C5HttpHelperGetResult(null, null, url3);

        //UserInfo userInfo = null;
        //using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(result)))
        //{
        //    DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(typeof(UserInfo));
        //    userInfo = (UserInfo)deseralizer.ReadObject(ms);// //反序列化ReadObject
        //}
    }

    public string C5HttpHelperGetResult(string pCookie, string pReferer, string pUrl)
    {
        HttpHelper httpHelper = new HttpHelper();
        HttpItem httpItem = new HttpItem();

        httpItem.Cookie = pCookie;
        httpItem.Referer = pReferer;
        httpItem.URL = pUrl;
        httpItem.Encoding = Encoding.UTF8;
        HttpResult httpResult = httpHelper.GetHtml(httpItem);

        return httpResult.Html;
    }

    public string C5HttpHelperPostResult(string pCookie, string pReferer, string pUrl, string pPostData)
    {
        HttpHelper httpHelper = new HttpHelper();
        HttpItem httpItem = new HttpItem();

        httpItem.Cookie = pCookie;
        httpItem.Referer = pReferer;
        httpItem.URL = pUrl;
        httpItem.Postdata = pPostData;

        HttpResult httpResult = httpHelper.GetHtml(httpItem);

        return httpResult.Html;
    }

    public System.Drawing.Image C5HttpHelperGetImage(string pCookie, string pReferer, string pUrl)
    {
        System.Drawing.Image image = null;

        HttpHelper httpHelper = new HttpHelper();
        HttpItem httpItem = new HttpItem();

        httpItem.ResultType = ResultType.Byte;
        httpItem.Encoding = Encoding.Default;

        httpItem.Cookie = pCookie;
        httpItem.Referer = pReferer;
        httpItem.URL = pUrl;

        HttpResult httpResult = httpHelper.GetHtml(httpItem);

        if (httpResult.StatusCode == System.Net.HttpStatusCode.OK)
        {
            image = byteArrayToImage(httpResult.ResultByte);
        }

        return image;
    }

    private System.Drawing.Image byteArrayToImage(byte[] Bytes)
    {
        MemoryStream ms = new MemoryStream(Bytes);
        System.Drawing.Image outputImg = System.Drawing.Image.FromStream(ms);
        return outputImg;
    }
}
